package cn.wolfcode.wolf2w.Job;

import cn.wolfcode.wolf2w.cache.redis.service.IStrategyStatesVoService;
import cn.wolfcode.wolf2w.cache.redis.vo.StrategyStatisVo;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.service.IStrategyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class RedisDataPersistenceJob {
    @Autowired
    private IStrategyStatesVoService strategyStatesVoService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private MongoTemplate mongoTemplate;

    //秒 分 小时 月份中的日期 月份 星期中的日期 年份
    //0 0 0 1/1 * ? 
    @Scheduled(cron="0/10 * * * * ?")
    public void redisDataPersistence() {
        //拼接好命令
        List<StrategyStatisVo> list =strategyStatesVoService.getvolist("*");
        for (StrategyStatisVo vo : list) {

            Query query = new Query();
            query.addCriteria(Criteria.where("_id").is(vo.getStrategyId()));
            Update update = new Update();
            update.set("viewnum",vo.getViewnum());
            update.set("replynum",vo.getReplynum());
            update.set("favornum",vo.getFavornum());
            update.set("sharenum",vo.getSharenum());
            update.set("thumbsupnum",vo.getThumbsupnum());
            mongoTemplate.updateMulti(query,update,Strategy.class);
        }
    }
}
